package generic;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * 泛型练习
 */
public class GenericExercise02 {

    public static void main(String[] args) {

        /*
            定义Employee类
            private 成员变量 name sal birthday 其中 birthday 为 MyDate类的对象
            为每一个属性定义 getter setter 方法
            重写 toString方法输出 name sal birthday
            MyDate类包含：private 成员变量  month day year;并为每一个属性定义 getter setter方法
            创建该类的3个对象 并把这些对象放入 ArrayList 集合中(ArrayList需要使用泛型来定义)，对集合中的元素进行排序并遍历输出
            排序方式：调用ArrayList的 sort方法，传入 Comparator 对象(使用泛型)，先按照 name排序, 如果 name 相同，则按生日日期的先后排序
         */

        Employee e1 = new Employee("张三", 4000.0, new MyDate(1999, 3, 20));
        Employee e2 = new Employee("李四", 3000.0, new MyDate(1999, 4, 20));
        Employee e3 = new Employee("王五", 5000.0, new MyDate(1999, 7, 20));
        Employee e4 = new Employee("王五", 3000.0, new MyDate(1999, 6, 20));

        ArrayList<Employee> list = new ArrayList<Employee>();
        list.add(e1);
        list.add(e2);
        list.add(e3);
        list.add(e4);

        list.sort(new Comparator<Employee>() {

            @Override
            public int compare(Employee o1, Employee o2) {
                int res = o1.getName().compareTo(o2.getName());
                if (res != 0) {
                    return res;
                }

                //可以封装到MyDate中
//                //如果 name相同 按生日日期先后排序
//                int yearMinus = o1.getBirthday().getYear() - o2.getBirthday().getYear();
//                if (yearMinus != 0) {
//                    return yearMinus;
//                }
//                int monthMinus = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
//                if (monthMinus != 0) {
//                    return monthMinus;
//                }
//                int dayMinus = o1.getBirthday().getDay() - o2.getBirthday().getDay();
//                if (dayMinus != 0) {
//                    return dayMinus;
//                }
                return o1.getBirthday().compareTo(o2.getBirthday());
            }
        });

        System.out.println("====排序后结果====");
        System.out.println(list);
    }
}

class Employee {

    private String name;

    private double sal;

    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate implements Comparable<MyDate> {

    private int year;

    private int month;

    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {    //把对年月日的比较放在这里，前者大返回正数，后者大返回负数
        //如果 name相同 按生日日期先后排序
        int yearMinus = this.year - o.getYear();
        if (yearMinus != 0) {
            return yearMinus;
        }
        int monthMinus = this.month - o.getMonth();
        if (monthMinus != 0) {
            return monthMinus;
        }
        int dayMinus = this.day - o.getDay();
        if (dayMinus != 0) {
            return dayMinus;
        }
        return 0;
    }
}